#include"Index.h"
#include"OccValue_Lam.cu"
__device__ bool BackwardSearch(BWT *bwt, unsigned int * Queries, unsigned int queryId, unsigned int lower, unsigned int upper, unsigned int * temp_start, unsigned int * temp_end)
{
	int i;
	char c;
	if(*temp_start > *temp_end)
		return false;

	for (i= (int)(upper-1); i>= (int)lower; i--)
	{
		c = getCharFromQueries(Queries, queryId, i);
		if(bwtCalc(bwt, c,temp_start, temp_end)== false)
			return false;
	}
	return true;
}


__device__ bool BackwardSearch(BWT *bwt, unsigned int * query, unsigned int lower, unsigned int upper, unsigned int * temp_start, unsigned int * temp_end)
{
	int i;
	char c;
	if(*temp_start > *temp_end)
		return false;

	for (i= (int)(upper-1); i>= (int)lower; i--)
	{
		c = getCharFromQueries(query, i);
		if(bwtCalc(bwt, c,temp_start, temp_end)== false)
			return false;
	}
	return true;
}


__device__
bool ForwardSearch(BWT* revbwt, unsigned int * query, 
					unsigned int lower, unsigned int upper, 
					unsigned int *temp_rev_start, unsigned int *temp_rev_end, 
					unsigned int *temp_start, unsigned int* temp_end,
					unsigned int *shareOccCount)
{
	char c;
#if (OCC_COUNT_PER_THREAD >= 12)
	unsigned int *occCount = shareOccCount;
#else
	unsigned int occCount[ALPHABET_SIZE];
#endif
	unsigned int occCount_start[ALPHABET_SIZE];
	unsigned int occCount_end[ALPHABET_SIZE];
	unsigned int i;

	for (i=lower; i<upper && *temp_start<= *temp_end; i++)
	{
		c = getCharFromQueries(query, i);
		bwtCalcAll(revbwt, temp_rev_start, temp_rev_end, occCount_start, occCount_end, occCount);
		bwtCalcWithOccValue(revbwt->countValue, c, occCount_start[c], occCount_end[c], temp_rev_start, temp_rev_end);
		*temp_end = *temp_end - occCount[c];
		*temp_start = *temp_end - (*temp_rev_end - *temp_rev_start);
	}
	if (*temp_start <= *temp_end && *temp_start > 0)
		return true;
	return false;
}

